package demiUser;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.BaseServlet;
import common.gFunction;

/**
 * Servlet implementation class DemiUserServlet
 */
@WebServlet("/userPage")
public class DemiUserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DemiUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    /*
     * 프로시져
     */
    public void doAnyProc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// TODO Auto-generated method stub
    	initServlet( request,  response);
    	System.out.println("/userPage");
    	if(reqFunc.equals("joinProc")){
    		/*회원가입요청처리*/
    		joinProc(request,response);
    	}
    	else if(reqFunc.equals("gotoJoinPage")){
    		/*회원가입페이지로*/
    		gotoJoinPage(request,response);
    	}
    	else if(reqFunc.equals("loginProc")){
    		/*로그인 요청 처리*/
    		loginProc(request,response);
    	}
    	else if(reqFunc.equals("logoutProc")){
    		/*로그아웃 요청 처리*/
    		logoutProc(request,response);
    	}

	    		
    	releaseServlet();
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doAnyProc(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doAnyProc(request,response);
	}
	
	
	/*
	 * 회원가입 프로시져
	 */
	protected void joinProc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String joinId=gFunction.getNotNull(request.getParameter("joinId"),"");
		String joinPass=gFunction.getNotNull(request.getParameter("joinPass"),"");
		String joinName=gFunction.getNotNull(request.getParameter("joinName"),"");
		String result=null;
		if(joinId !="" && joinPass !="" && joinName !="" ){
			if(demiUser.isThereId(joinId)){
				System.out.println("가입실패");
				request.setAttribute("message", "아이디 중복");
				(request.getRequestDispatcher("/user/joinPage.jsp")).forward(request, response);
				
			}
			else{
			/*가입이 완료된경우*/
			demiUser.doJoin(joinId, joinId, joinPass);
			System.out.println("가입성공");
			request.setAttribute("message", "가입완료");
			(request.getRequestDispatcher("/mainPage.jsp")).forward(request, response);
			}
			
		}
		else{
			System.out.println("가입실패");
			request.setAttribute("message", "모두 입력해주세요");
			(request.getRequestDispatcher("/user/joinPage.jsp")).forward(request, response);
		}
		
	}
	/*
	 * 회원가입페이지로
	 */
	protected void gotoJoinPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		(request.getRequestDispatcher("/user/joinPage.jsp")).forward(request, response);
	}
	/*
	 * 로그인요청 Ajax
	 */
	protected void loginProc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		int result=0;
		String tempId=(String)request.getParameter("userId");
		String tempPass=(String)request.getParameter("userPass");;
		result=demiUser.userLogin(tempId, tempPass);
		switch(result){
		case 0:/*로그인성공*/
			System.out.println("로그인성공");
			(request.getRequestDispatcher("/user/logined.jsp")).forward(request, response);
			break;
		case 1:/*패스워드 틀림*/
			System.out.println("패스워드 틀림");
			(request.getRequestDispatcher("/user/login.jsp")).forward(request, response);
		
			break;
		case 2:/*아이디없음*/
			System.out.println("아이디없음");
			(request.getRequestDispatcher("/user/login.jsp")).forward(request, response);
		
			break;
		}
	}
	/*
	 * 로그아웃 Ajax
	 */
	protected void logoutProc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
			int result=0;
			session.removeAttribute("userId");
			session.removeAttribute("userGrade");
			(request.getRequestDispatcher("/user/login.jsp")).forward(request, response);
	}
	

}
